package liang.jiaxin.com.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import liang.jiaxin.com.utils.SHA256Util;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

/**
 * @User: Jrxsr
 * WxController
 * on 2019/5/9.
 **/
@RestController
@Api(value = "接口", description = "接口")
public class WxController {
    @GetMapping("/token")
    @ApiOperation(value = "公众号验证token" , produces = MediaType.APPLICATION_JSON_VALUE)
    public String getToken(@RequestParam Map map) {
        System.out.println(map);
        if (CollectionUtils.isEmpty(map)) {
            return "获取Token失败";
        }
        ArrayList<String> arrayList = new ArrayList();
        arrayList.add(String.valueOf(map.get("timestamp")));
        arrayList.add(String.valueOf(map.get("nonce")));
        arrayList.add("weixin");

        Collections.sort(arrayList);

        String str = "";
        for (String item:arrayList) {
            str+=item;
        }
        System.out.println(str);
        System.out.println(SHA256Util.getSha1(str));
        System.out.println(map.get("signature"));
        if (SHA256Util.getSha1(str).equals(map.get("signature"))){
            return String.valueOf(map.get("echostr"));
        }else{
            return "";
        }
    }

    @PostMapping("/token")
    @ApiOperation(value = "公众号获取消息")
    public String getMsg(@RequestParam String string) {
        System.out.println(string);

        return "";
    }
}
